package com.shoulder.boot.ops.conf;

import com.shoulder.boot.ops.service.MachineService;
import com.shoulder.boot.ops.ws.SSHSocketHandler;
import com.shoulder.boot.ops.ws.SSHSocketInterceptor;
import com.shoulder.ssh.properties.SSHProperties;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Data
@AllArgsConstructor
@Configuration
@EnableWebSocket
public class SSHSocketConfigurer implements WebSocketConfigurer {

    private final MachineService machineService;
    private final SSHProperties sshProperties;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        SSHSocketHandler socketHandler = new SSHSocketHandler();
        socketHandler.setMachineService(machineService);
        socketHandler.setProperties(sshProperties);
        registry.addHandler(socketHandler, "/ws/ssh/{token}").addInterceptors(new SSHSocketInterceptor())
                .setAllowedOrigins("*");
    }
}
